package ahxu.search.db;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.sql.DataSource;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

public class UnFinalResultDao implements IUnFinalResultDao{
	private DataSource dataSource;
	public static final String table="UNFINAL_RESULT";
	public UnFinalResultDao(DataSource dataSource){
		this.dataSource=dataSource;
	}

	public PageResults<UnFinalResult> findResults(String crawlId,String eventObject,String eventAction,int pageNo,int pageSize) throws FinalResultDaoException{
		try{
			PageResults<UnFinalResult> ret=new PageResults<UnFinalResult>();
			ret.setPageSize(pageSize);
			ret.setCurrentPage(pageNo);
				String sql="select * from " +table+ " where 1=1 ";
				String totalSql= "select count(*) from " +table+ " where 1=1 ";
				String querySql="";
				Map queryValue=new HashMap();
				if(!StringUtils.isEmpty(crawlId)){
					querySql+=" and CRAWL_ID like :crawlId ";
					queryValue.put("crawlId", crawlId);
				}
				if(!StringUtils.isEmpty(eventObject)){
					querySql+=" and EVENT_OBJECT like :eventObject ";
					queryValue.put("eventObject", eventObject);
				}
				if(!StringUtils.isEmpty(eventAction)){
					querySql+=" and EVENT_ACTION like :eventAction ";
					queryValue.put("eventAction", eventAction);
				}
				//querySql+=" limit "
				SimpleJdbcTemplate simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
				
			    int total=simpleJdbcTemplate.queryForInt(totalSql+querySql, queryValue);
			    
			    ret.setTotalCount(total);
			    querySql+=" limit " +ret.getPageSize()+" offset "+(ret.getResultsFrom()-1);
			    ParameterizedRowMapper<UnFinalResult> mapper = new ParameterizedRowMapper<UnFinalResult>() {
				    
			        // notice the return type with respect to Java 5 covariant return types
			        public UnFinalResult mapRow(ResultSet rs, int rowNum) throws SQLException {
			        	UnFinalResult obj = new UnFinalResult();
			        	obj.setId(rs.getString("id"));
			        	obj.setCrawlId(rs.getString("CRAWL_ID"));
			        	obj.setEventObject(rs.getString("EVENT_OBJECT"));
			        	obj.setEventAction(rs.getString("EVENT_ACTION"));
			            
			            return obj;
			        }
			    };
				List<UnFinalResult> retResults=simpleJdbcTemplate.query(sql+querySql, mapper, queryValue);
				ret.setResults(retResults);

			
			return ret;
		}catch(Exception e){
			throw new FinalResultDaoException(e);
		}
	}

	public String save(String crawlId, String eventObject, String eventAction) throws FinalResultDaoException {
		try{
			String id=null;
			if(this.dataSource!=null){
				String insertSQL="insert into " +table+
				"(ID,CRAWL_ID,EVENT_OBJECT,EVENT_ACTION)" +
				"values" +
				"(?,?,?,?)";
				SimpleJdbcTemplate simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
				id=UUID.randomUUID().toString();
				simpleJdbcTemplate.update(insertSQL,
						id,
						crawlId,
						eventObject,
						eventAction);
			}
			
			return id;
		}catch(Exception e){
			//logger.error(e.getMessage());
			throw new FinalResultDaoException(e);
		}
	}


}
